package com.service.socket.process;

import org.jboss.netty.channel.Channel;

import com.service.cache.CacheManager;
import com.service.other.Logger;
import com.service.socket.protocol.CmdWord;
import com.service.socket.protocol.Packet;

/**
 * A0指令，链路操作
 * @author sxc
 *
 */
public class R_Class_A0 extends R_Class_Abstract {
	
	public R_Class_A0(Channel channel, Packet packet) {
		super(channel, packet);
		this.vehicle = CacheManager.instance().getVehicle(packet.getDeviceNo());
		this.road_vehicle = CacheManager.instance().getVehicle_Road(packet.getDeviceNo());
		this.hookArmCase = CacheManager.instance().getHookArmCase(packet.getDeviceNo());
	}

	@Override
	public void execute() {
		try {
			//System.out.println("dtu -> server : " + packet.toString());
			if(CmdWord.COWORD_REG_LOGIN.equals(packet.cmdWord)){
				method_00();
			}else if(CmdWord.COWORD_HEARTBEAT.equals(packet.cmdWord)){
				method_01();
			}
		} catch (Exception e) {
			Logger.err(e);
		}
	}
	
	public void method_00(){

		if(vehicle == null && road_vehicle == null && hookArmCase == null){
			packet.status = CmdWord.STATUS_FAIL;
			packet.dataArea = null;
			SendCommand.replyDtu(channel,packet);
			Logger.err("device ["+packet.getDeviceNo()+"] regist fail : vehicle no bind");
			channel.close().awaitUninterruptibly();
			return;
		}
		
		String gpsKey = "";
		if(vehicle != null){
			gpsKey = vehicle.device.gpsKey;
		} else if(hookArmCase != null){
			gpsKey = hookArmCase.device.gpsKey;
		}
		if(road_vehicle == null){
			if(gpsKey.equals(packet.getAuthCode())){
				packet.status = CmdWord.STATUS_FAIL;
				packet.dataArea = null;
				SendCommand.replyDtu(channel,packet);
				Logger.err("device ["+packet.getDeviceNo()+"] regist fail on [AuthCode]:" + packet.getAuthCode() + " not equals " + gpsKey);
				channel.close().awaitUninterruptibly();
				return;
			}
		}
		if (!packet.isCrcValid()){
			packet.status = CmdWord.STATUS_FAIL;
			packet.dataArea = null;
			SendCommand.replyDtu(channel,packet);
			Logger.err("device ["+packet.getDeviceNo()+"] regist fail on [CrcValid]:" + packet.isCrcValid());
			channel.close().awaitUninterruptibly();
			return;
		}
		if (CacheManager.instance().activeChannel.containsKey(channel.getId())) {
			CacheManager.instance().activeChannel.get(channel.getId()).close();
			Logger.println("device ["+packet.getDeviceNo()+"] disconnect the old connection");
		}
		CacheManager.instance().activeChannel.put(channel.getId(), channel);
		CacheManager.instance().sessionMap.put(packet.getDeviceNo(), channel.getId());
		packet.status = CmdWord.STATUS_SUCCESS;
		packet.dataArea = null;
		SendCommand.replyDtu(channel,packet);
		Logger.println("device ["+packet.getDeviceNo()+"] regist successed");
		SendCommand.sendCtrlUser(packet);
		if(vehicle != null && CacheManager.instance().afterFaSet.contains(vehicle.device.deviceNo)){
			CacheManager.instance().afterFaSet.remove(vehicle.device.deviceNo);
			//下发垃圾数据 车辆only
			//添加一个任务
			R_Class_FF.addjob_FF02(vehicle.device.deviceNo, "02");
			//执行一个任务（串行化）
			System.out.println("");
			System.out.println("A0下发同步指令start");
			R_Class_FF.dojob_FF02(vehicle.device.deviceNo, "02");						
		}
	}
	
	public void method_01(){
		packet.status = CmdWord.STATUS_SUCCESS;
		packet.dataArea = null;
		SendCommand.replyDtu(channel,packet);
	}

}
